package com.wowo.ffms.service;

import com.wowo.ffms.dao.UserDao;
import com.wowo.ffms.entity.Account;
import com.wowo.ffms.entity.User;
import com.wowo.ffms.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;
import java.util.List;

public class UserService {
    SqlSession sqlSession;
    UserDao userDao;

    public void init() throws IOException {
        SqlSessionFactory sqlSessionFactory = MybatisUtil.getSqlSessionFactory();
        sqlSession = sqlSessionFactory.openSession();
        userDao = sqlSession.getMapper(UserDao.class);

    }
    public String login(String name,String pwd,String code,String vcode) throws IOException {
        init();
        User user = userDao.getUserByName(name);

        String result = "2";
        if(user!=null&&pwd.equals(user.getPwd())){
            result = "1";
            if(!code.equals(vcode)){
                result = "3";
            }
        }
        sqlSession.commit();
        sqlSession.close();
        return result;

    }

    public User getUserByName(String name) throws IOException {
        init();
        User user = userDao.getUserByName(name);
        return user;
    }

}
